<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        v-permission="'txMode:add'"
        class="filter-item"
        style="margin-left: 10px;"
        @click="openAddWithdrawMode"
        type="success"
        icon="el-icon-plus"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column
        prop="rowId"
        label="ID"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="modeCode"
        label="方式编码"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="modeName"
        label="方式名称"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column prop="type" label="是否免费" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.WITHDRAW_MODE_TYPE[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="fee"
        label="每笔手续费"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="freeDate"
        label=" 每月免手续费日"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="yearFreeTimes"
        label="每年最大免手续费次数"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column prop="status" label="状态" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.WITHDRAW_MODE_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'txMode:update'"
            type="primary"
            size="mini"
            @click="editWithdrawModeForm(scope.row)"
            title="编辑"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            v-permission="'txMode:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>
    <!-- 修改新增推送消息弹框 -->
    <el-dialog
      :title="`${editForm.rowId ? '编辑' : '新增'}提现方式`"
      :visible.sync="dialogFormVisible"
      width="40%"
    >
      <el-form
        :model="editForm"
        label-width="200px"
        :rules="editFormRules"
        ref="editForm"
      >
        <el-form-item label="方式名称:" prop="txModeName">
          <el-select
            v-model="editForm.txModeName"
            placeholder="方式名称"
            style="width: 100%;"
            @change="txMode"
          >
            <el-option
              v-for="(val, key) in Dicts.WITHDRAW_CASH_TYPE"
              :key="key"
              :label="val"
              :value="key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否免费:" prop="type">
          <el-select
            v-model="editForm.type"
            placeholder="是否免费"
            style="width: 100%;"
            :disabled="true"
          >
            <el-option
              v-for="(val, key) in Dicts.WITHDRAW_MODE_TYPE"
              :key="key"
              :label="val"
              :value="key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="每笔手续费:" prop="fee">
          <el-input v-model="editForm.fee" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="每月免手续费日:" prop="freeDate">
          <el-input
            v-model="editForm.freeDate"
            auto-complete="off"
            placeholder="例如：1、15"
          ></el-input>
        </el-form-item>
        <el-form-item label="每年最大免手续费次数:" prop="yearFreeTimes">
          <el-input
            v-model="editForm.yearFreeTimes"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态:" prop="status">
          <el-select
            v-model="editForm.status"
            placeholder="状态"
            style="width: 100%;"
          >
            <el-option
              v-for="(val, key) in Dicts.WITHDRAW_MODE_STATUS"
              :key="key"
              :label="val"
              :value="key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveMessage(editForm.rowId)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryWithdrawMode,
  addOrEditWithdrawMode,
  deleteWithdrawMode
} from '@/api/account'

export default {
  name: 'withdrawMode',
  data() {
    var checkFreeDate = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入续费日'))
      } else {
        var str = value.split('、')
        if (str.length === 1) {
          if (!/^((0?[1-9])|((1|2)[0-9])|30|31)$/.test(str)) {
            callback(new Error('请输入1-31之间的数字'))
          } else {
            callback()
          }
        } else {
          for (var i = 0; i < str.length; i++) {
            if (!/^((0?[1-9])|((1|2)[0-9])|30|31)$/.test(str[i])) {
              callback(new Error('请输入1-31之间数字'))
            }
          }
          callback()
        }
      }
    }
    return {
      dialogFormVisible: false,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        title: undefined
      },
      editForm: {
        rowId: 0,
        modeCode: '',
        modeName: '',
        fee: '',
        freeDate: '',
        yearFreeTimes: '',
        status: ''
      },
      editFormRules: {
        type: [{ required: true, message: '请选择免费方式', trigger: 'blur' }],
        txModeName: [
          { required: true, message: '请输入方式名称', trigger: 'blur' }
        ],
        fee: [{ required: true, message: '请输入每笔手续费', trigger: 'blur' }],
        freeDate: [
          { required: true, validator: checkFreeDate, trigger: 'blur' }
        ],
        yearFreeTimes: [
          {
            required: true,
            message: '请输入每年最大免手续费次数',
            trigger: 'blur'
          }
        ],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      },
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryWithdrawMode(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    openAddWithdrawMode() {
      this.editForm = {}
      this.dialogFormVisible = true
      this.$refs.editForm && this.$refs.editForm.resetFields()
    },
    txMode(value) {
      if (value === 'free') {
        this.editForm.type = '1'
      } else {
        this.editForm.type = '0'
      }
    },
    saveMessage(rowId) {
      this.editForm.modeCode = this.editForm.txModeName
      if (this.editForm.txModeName === 'free') {
        this.editForm.modeName = '免费提现'
      } else {
        this.editForm.modeName = '普通提现（T+N)'
      }
      this.$refs.editForm.validate(valid => {
        if (valid) {
          addOrEditWithdrawMode(this.editForm).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    editWithdrawModeForm({
      rowId,
      modeCode,
      modeName,
      fee,
      freeDate,
      yearFreeTimes,
      status,
      type
    }) {
      this.openAddWithdrawMode()
      this.editForm = {
        rowId: rowId,
        modeCode: modeCode,
        modeName: modeName,
        txModeName: modeCode,
        fee: fee,
        freeDate: freeDate,
        yearFreeTimes: yearFreeTimes,
        status: status,
        type: type
      }
    },
    handleDelete(rowId) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWithdrawMode(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
